package com.training.java.redbox;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class UserForm extends ActionForm {
	
	private String userName;
	private String password;
	private boolean formIsLoaded = false;
	
	
	public boolean isFormIsLoaded() {
		return formIsLoaded;
	}
	public void setFormIsLoaded(boolean formIsLoaded) {
		this.formIsLoaded = formIsLoaded;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		
		if (formIsLoaded) {	
		if (userName==null || userName.trim().length() == 0) {
			errors.add("userName", new ActionMessage("userName.required"));
		}
		
		if (password==null || password.trim().length() == 0) {
			errors.add("password", new ActionMessage("password.required"));
		}
		}
		
		return errors;
	}
	
}
